<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增站内消息')"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-notice-add">
        <div class="form-group">
            <label class="col-sm-2 control-label">消息类型：</label>
            <div class="col-sm-10">
                <div class="input-group">
                    <input name="messageType" id="messageType" type="hidden">
                    <input name="messageTypeName" onclick="selectTypeTree()" id="messageTypeName" type="text"
                           placeholder="请选择消息分类" class="form-control" readonly>
                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">消息标题：</label>
            <div class="col-sm-10">
                <input id="noticeTitle" name="noticeTitle" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">接收人：</label>
            <div class="col-sm-10 select-table table-striped" style="width: auto;margin-left: 15px;">
                <input name="toUserIds" type="hidden">
                <table id="bootstrap-table"></table>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">消息内容：</label>
            <div class="col-sm-10">
                <input id="noticeContent" name="noticeContent" type="hidden">
                <div class="summernote"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">消息状态：</label>
            <div class="col-sm-10">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
                    <input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}"
                           th:checked="${dict.default}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: summernote-js"/>
<script th:inline="javascript">
    var datas = [[${@dict.getType('sys_normal_disable')}]];
    var prefix = ctx + "back/message/notice";

    $(function () {
        var options = {
            url: ctx + "system/user/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            importTemplateUrl: prefix + "/importTemplate",
            sortName: "createTime",
            sortOrder: "desc",
            modalName: "用户",
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            clickToSelect: true,
            rememberSelected: true,
            columns: [{
                field: 'state',
                checkbox: true
            },
                {
                    field: 'userId',
                    title: '用户ID',
                    visible: false
                },
                {
                    field: 'loginName',
                    title: '登录名称',
                    sortable: true
                },
                {
                    field: 'userName',
                    title: '用户名称'
                },
                {
                    field: 'email',
                    title: '邮箱'
                },
                {
                    field: 'phonenumber',
                    title: '手机'
                },
                {
                    field: 'status',
                    title: '用户状态',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(datas, value);
                    }
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                }]
        };
        $.table.init(options);
    });

    $('.summernote').summernote({
        placeholder: '请输入站内消息内容',
        height: 192,
        lang: 'zh-CN',
        followingToolbar: false,
        callbacks: {
            onImageUpload: function (files) {
                sendFile(files[0], this);
            }
        }
    });

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.url, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

    // 选择消息分类树
    function selectTypeTree() {
        var typeId = $("#messageType").val();
        var typeId = $.common.isEmpty(typeId) ? "100" : $("#messageType").val();
        var url = ctx + "message/type/selectTypeTree/" + typeId;
        var options = {
            title: '选择消息分类',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        if ($.tree.notAllowParents(tree)) {
            var body = layer.getChildFrame('body', index);
            $("#messageType").val(body.find('#treeId').val());
            $("#messageTypeName").val(body.find('#treeName').val());
            layer.close(index);
        }
    }

    $("#form-notice-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var rows = $.table.selectFirstColumns();
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一个接收人");
                return;
            }
            var sHTML = $('.summernote').summernote('code');
            $("#noticeContent").val(sHTML);
            $("input[name='toUserIds']").val(rows.join())
            $.operate.save(prefix + "/add", $('#form-notice-add').serialize());
        }
    }
</script>
</body>
</html>
